package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JComboBox;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import entities.Klasse;
import entities.Schueler;
import persistence.Schulverwaltung;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class Hauptfenster extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JList<Schueler> jlSchueler;
	private JComboBox<Klasse> cbKlassen;
	private JTextField tfSchuelerName;
	private JTextField tfKlassenName;
	
	private Schulverwaltung verwaltung;
	private DefaultListModel<Schueler> schuelerModel = new DefaultListModel<>();
	private DefaultComboBoxModel<Klasse> klassenModel = new DefaultComboBoxModel<>();
	private JLabel lbSchuelerAnzahl;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable() {
			public void run()
			{
				try
				{
					Hauptfenster frame = new Hauptfenster();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Hauptfenster()
	{
		try
		{
			initFrame();
			verwaltung = new Schulverwaltung();
			jlSchueler.setModel(schuelerModel);
			cbKlassen.setModel(klassenModel);
			ladeKlassen();
			ladeSchueler();
		} catch (Exception e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
		
	}
	
	private void ladeKlassen()
	{
		try
		{
			klassenModel.removeAllElements();
			for (Klasse k : verwaltung.alleKlassen())
				klassenModel.addElement(k);
		} catch (Exception e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	private void ladeSchueler()
	{
		schuelerModel.removeAllElements();
		Klasse k = (Klasse) cbKlassen.getSelectedItem();
		if (k != null)
		{
			for (Schueler s : k.getSchuelerListe())
				schuelerModel.addElement(s);
		}
	}
	
	
	private void aktualisiereFelder()
	{
		Klasse k = (Klasse) cbKlassen.getSelectedItem();
		if (k != null)
		{
			tfKlassenName.setText(k.getBezeichnung());
			lbSchuelerAnzahl.setText(String.valueOf(k.getSchuelerListe().size()));
		}
		else
		{
			tfKlassenName.setText("");
			lbSchuelerAnzahl.setText("");
		}
		Schueler s = jlSchueler.getSelectedValue();
		if (s != null)
		{
			tfSchuelerName.setText(s.getName());
		}
		else
		{
			tfSchuelerName.setText("");
		}
	}
	
	
	
	private void aktualisiereObjekte()
	{
		Klasse k = (Klasse) cbKlassen.getSelectedItem();
		if (k != null)
		{
			k.setBezeichnung(tfKlassenName.getText());
		}
		Schueler s = jlSchueler.getSelectedValue();
		if (s != null)
		{
			s.setName(tfSchuelerName.getText());
		}
		
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initFrame()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JSplitPane splitPane = new JSplitPane();
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);
		
		jlSchueler = new JList();
		jlSchueler.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				do_jlSchueler_valueChanged(arg0);
			}
		});
		scrollPane.setViewportView(jlSchueler);
		
		JPanel panel_1 = new JPanel();
		splitPane.setRightComponent(panel_1);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblSchlername = new JLabel("Sch\u00FClername");
		panel_1.add(lblSchlername);
		
		tfSchuelerName = new JTextField();
		panel_1.add(tfSchuelerName);
		tfSchuelerName.setColumns(10);
		
		JLabel lblKlassenname = new JLabel("Klassenname");
		panel_1.add(lblKlassenname);
		
		tfKlassenName = new JTextField();
		panel_1.add(tfKlassenName);
		tfKlassenName.setColumns(10);
		
		JButton btnNeuerschueler = new JButton("neuerSchueler");
		btnNeuerschueler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnNeuerschueler_actionPerformed(arg0);
			}
		});
		
		JLabel lblAnzahlDerSchler = new JLabel("Anzahl der Sch\u00FCler");
		panel_1.add(lblAnzahlDerSchler);
		
		lbSchuelerAnzahl = new JLabel("");
		panel_1.add(lbSchuelerAnzahl);
		panel_1.add(btnNeuerschueler);
		
		JButton btnNeueklasse = new JButton("neueKlasse");
		btnNeueklasse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnNeueklasse_actionPerformed(e);
			}
		});
		panel_1.add(btnNeueklasse);
		
		JButton btnnderungenSpeichern = new JButton("\u00C4nderungen speichern");
		btnnderungenSpeichern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnnderungenSpeichern_actionPerformed(e);
			}
		});
		panel_1.add(btnnderungenSpeichern);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		cbKlassen = new JComboBox();
		cbKlassen.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				do_cbKlassen_itemStateChanged(arg0);
			}
		});
		panel.add(cbKlassen);
	}

	protected void do_btnNeuerschueler_actionPerformed(ActionEvent arg0) 
	{
		try
		{
			Klasse k = (Klasse) cbKlassen.getSelectedItem();
			if (k != null)
			{
				Schueler s = new Schueler();
				k.add(s);
				s.setName(tfSchuelerName.getText());
				verwaltung.neu(s);
				schuelerModel.addElement(s);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	protected void do_btnNeueklasse_actionPerformed(ActionEvent e) 
	{
		try
		{
			Klasse k = new Klasse();
			k.setBezeichnung(tfKlassenName.getText());
			verwaltung.neu(k);
			klassenModel.addElement(k);
		} catch (Exception e1)
		{
			e1.printStackTrace();
			JOptionPane.showMessageDialog(this, e1.getMessage());
		}
	}
	
	protected void do_cbKlassen_itemStateChanged(ItemEvent arg0) 
	{
		ladeSchueler();
		aktualisiereFelder();
	}
	
	protected void do_jlSchueler_valueChanged(ListSelectionEvent arg0) 
	{
		aktualisiereFelder();
	}
	
	protected void do_btnnderungenSpeichern_actionPerformed(ActionEvent e) 
	{
		try
		{
			aktualisiereObjekte();
			verwaltung.speichern();
		} catch (Exception e2)
		{
			e2.printStackTrace();
			JOptionPane.showMessageDialog(this, e2.getMessage());
		}
	}
}
